package com.shen.module_room

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.shen.module_room.bean.ChatRecordBean
import com.shen.module_room.bean.UserInfoBean

class RoomMainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.room_activity_main)

        findViewById<TextView>(R.id.tv_add).setOnClickListener {
            val random = (0..10L).random()
            AppDataBase.getDBInstance().getUserInfoDao()
                //.insert(UserInfoBean(random, random.toString(), random.toString()))
                //.insert(UserInfoBean(random, random.toString(), random.toString(), random.toString()))
                .insert(UserInfoBean(random, random.toString(), random.toString(), random.toString(), true))

            AppDataBase.getDBInstance().getChatRecordDao()
                .insert(ChatRecordBean(random, random.toString(), true))

            var temp = ""
            val list = AppDataBase.getDBInstance().getUserInfoDao().getAllUser()
            list?.forEach {
                temp += "${it.toString()}\n"
            }

            val list1 = AppDataBase.getDBInstance().getChatRecordDao().getAllChat()
            list1?.forEach {
                temp += "${it.toString()}\n"
            }

            findViewById<TextView>(R.id.tv_content).text = temp
        }

        findViewById<TextView>(R.id.tv_deleteAll).setOnClickListener {
            AppDataBase.getDBInstance().getUserInfoDao().deleteAll()
            AppDataBase.getDBInstance().getChatRecordDao().deleteAll()

            var temp = ""
            val list = AppDataBase.getDBInstance().getUserInfoDao().getAllUser()
            list?.forEach {
                temp += "${it.toString()}\n"
            }
            val list1 = AppDataBase.getDBInstance().getChatRecordDao().getAllChat()
            list1?.forEach {
                temp += "${it.toString()}\n"
            }
            findViewById<TextView>(R.id.tv_content).text = temp
        }

    }
}